package com.kzw.config.request;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

/**
 * 解决请求url中的双斜杠问题
 */
@Component
public class UriFormatFilter extends OncePerRequestFilter {
	
	@Override
	protected void doFilterInternal(HttpServletRequest req, HttpServletResponse resp, FilterChain chain)
			throws ServletException, IOException {

		String uri = req.getRequestURI();
		final String newUri = uri.replace("//", "/");
		req = new HttpServletRequestWrapper(req) {
			@Override
			public String getRequestURI() {
				return newUri;
			}
		};

		chain.doFilter(req, resp);
	}
}
